<template>
<div>
<!--    <loginLogo/>-->
  <img src="../../static/img/background.jpg" class="background">
    <div class="signUp">
        <div class="signUp-head">
            <span>帐号登录</span>
        </div>
        <el-form :model="loginForm" ref="loginForm" label-width="70px" class="demo-ruleForm" :rules="rules">
            <el-form-item prop="username" label="用户名">
                <el-input v-model="loginForm.username" placeholder="用户名"></el-input>
            </el-form-item>
            <el-form-item prop="password" label="密码">
                <el-input type="password" v-model="loginForm.password" placeholder="密码"></el-input>
            </el-form-item>

            <div class="login-btn">
                <el-button @click="goSignUp">注册</el-button>
                <el-button type="primary" @click="handleLoginIn">登录</el-button>
            </div>
        </el-form>
    </div>
</div>
</template>
<script>
import loginLogo from '../components/LoginLogo'
import {mixin} from '../mixins'
import {myLogin,usermation} from "../api/user";

export default {
    name: 'login-in',
    mixins: [mixin],
    components: {
        loginLogo
    },
    data() {
        return {
            loginForm: {
                username: '',       //用户名
                password: '',       //密码
            },
            rules: {
                username: [
                    { required: true, trigger: 'blur',message: '请输入用户名' }
                ],
                password: [
                    { required: true, trigger: 'blur',message: '请输入密码' }
                ]
            }
        }
    },
    mounted() {
        this.changeIndex('登录');
    },
    methods:{
        handleLoginIn(){
          let _this = this;
          myLogin(this.loginForm).then(res=>{
            if (res.data.code==200){
              this.$message.success(res.data.message)
              this.$store.commit('setLoginIn',true);
              window.sessionStorage.setItem("Authorization-client",JSON.stringify(res.data.obj.tokenHead+" "+res.data.obj.token))
              //获取当前登录用户的信息
              usermation().then(res=>{
                // console.log(res.data.obj.headUrl)
                _this.$store.commit('setUserId',res.data.obj.id);
                _this.$store.commit('setUsername',res.data.obj.name);
                _this.$store.commit('setAvator',res.data.obj.headUrl);

              })
              setTimeout(function(){
                _this.changeIndex('首页');
                _this.$router.push({path: '/'});
                // _this.changeIndex('首页');
                // this.$router.go(-1);
                // window.history.back();
              },100);}else {
              this.$message.error(res.data.message)

            }
          }).catch(error=>{
            console.log(error)
          })
        },
        goSignUp(){
            this.changeIndex('注册');
            this.$router.push({path: '/sign-up'});
        },
        changeIndex(value){
            this.$store.commit('setActiveName',value);
        }
    }
}
</script>

<style lang="scss" scoped>
.signUp{
    margin-top: 230px;
    margin-right: 500px;
}
.background{
           width:100%;
           height: 100%;
           background: yellow;
           position: absolute;
}
@import '../assets/css/sign-up.scss';
</style>
